#include <stdio.h>

#include <string.h>



void get_next(char t[], int next[]) {

    int i = 1, j = 0;

    next[1] = 0;

    while (i < strlen(t + 1)) {

        if (j == 0 || t[i] == t[j]) {

            i++;

            j++;

            next[i] = j;

        } else {

            j = next[j];

        }

    }

}



void get_nextval(char t[], int next[], int nextval[]) {

    int i = 1;

    nextval[1] = 0;

    while (i < strlen(t + 1)) {

        if (next[i] == 0) {

            nextval[i] = 0;

        } else if (t[i] == t[next[i]]) {

            nextval[i] = nextval[next[i]];

        } else {

            nextval[i] = next[i];

        }

        i++;

    }

}



int main() {

    char t[] = " abcaabbabcab";

    int next[100], nextval[100];

    get_next(t, next);

    get_nextval(t, next, nextval);

    for (int i = 1; i <= strlen(t + 1); i++) {

        printf("next[%d]=%d ", i, next[i]);

    }

    printf("\n");

    for (int i = 1; i <= strlen(t + 1); i++) {

        printf("nextval[%d]=%d ", i, nextval[i]);

    }

    return 0;

}